package com.mc.fil.service.impl;

import java.util.List;

import com.mc.common.core.domain.model.LoginUser;
import com.mc.common.utils.DateUtils;
import com.mc.common.utils.ServletUtils;
import com.mc.fil.domain.FilMultimedia;
import com.mc.fil.mapper.FilMultimediaMapper;
import com.mc.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mc.fil.mapper.FilPhenologicalPhaseMapper;
import com.mc.fil.domain.FilPhenologicalPhase;
import com.mc.fil.service.IFilPhenologicalPhaseService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物候期 Service业务层处理
 *
 * @author mc
 * @date 2021-04-14
 */
@Service
public class FilPhenologicalPhaseServiceImpl implements IFilPhenologicalPhaseService
{
    @Autowired
    private FilPhenologicalPhaseMapper filPhenologicalPhaseMapper;
    @Autowired
    private FilMultimediaMapper multimediaMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询物候期
     *
     * @param filId 物候期 ID
     * @return 物候期
     */
    @Override
    public FilPhenologicalPhase selectFilPhenologicalPhaseById(String filId)
    {
        FilPhenologicalPhase phenologicalPhase = filPhenologicalPhaseMapper.selectFilPhenologicalPhaseById(filId);
        return phenologicalPhase;
    }

    /**
     * 查询物候期 列表
     *
     * @param filPhenologicalPhase 物候期
     * @return 物候期
     */
    @Override
    public List<FilPhenologicalPhase> selectFilPhenologicalPhaseList(FilPhenologicalPhase filPhenologicalPhase)
    {
        List<FilPhenologicalPhase>  list = filPhenologicalPhaseMapper.selectFilPhenologicalPhaseList(filPhenologicalPhase);
        for(FilPhenologicalPhase fil : list){
            FilMultimedia multimedia = new FilMultimedia();
            multimedia.setFilId(fil.getId());
            multimedia.setGenre("filPhenologicalPhase");
            fil.setPhotos(multimediaMapper.selectFilMultimediaList(multimedia));
        }
        return list;
    }

    /**
     * 新增物候期
     *
     * @param filPhenologicalPhase 物候期
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFilPhenologicalPhase(FilPhenologicalPhase filPhenologicalPhase)
    {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        filPhenologicalPhase.setCreateUserId(loginUser.getUsername());
        filPhenologicalPhase.setCreateTime(DateUtils.getNowDate());
        filPhenologicalPhaseMapper.insertFilPhenologicalPhase(filPhenologicalPhase);
        List<FilMultimedia> list = filPhenologicalPhase.getPhotos();
        for(FilMultimedia filMultimedia : list){
            filMultimedia.setFilId(filPhenologicalPhase.getId());
            filMultimedia.setCreatedBy(loginUser.getUsername());
            filMultimedia.setCreatedTime(DateUtils.getNowDate());
            filMultimedia.setGenre("filPhenologicalPhase");
            multimediaMapper.insertFilMultimedia(filMultimedia);
        }
        return 1;
    }

    @Override
    public int updateFilPhenologicalPhase(FilPhenologicalPhase filPhenologicalPhase) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        List<FilMultimedia> list = filPhenologicalPhase.getPhotos();
        for(FilMultimedia filMultimedia : list){
            filMultimedia.setFilId(filPhenologicalPhase.getId());
            filMultimedia.setCreatedBy(loginUser.getUsername());
            filMultimedia.setCreatedTime(DateUtils.getNowDate());
            filMultimedia.setGenre("filPhenologicalPhase");
            multimediaMapper.insertFilMultimedia(filMultimedia);
        }
        return filPhenologicalPhaseMapper.updateFilPhenologicalPhase(filPhenologicalPhase);
    }

}